//
//  GLViewController.mm
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
//

#import "GLViewController.h"
#import "GLView.h"
#import "glues.h"

@interface GLViewController ()

@property(nonatomic, retain) EAGLContext *context;
@property(nonatomic, assign) CADisplayLink *dispLink;

-(void)drawFrame;

@end


@implementation GLViewController

@synthesize animating;
@dynamic frameInterval;

@synthesize context;
@synthesize dispLink;

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	[super viewDidUnload];
}

- (void)dealloc {
	[context release];
	[super dealloc];
}

-(id)init {
	self = [super init];
	if (self) {
		EAGLContext *ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
		self.context = ctx;
		[ctx release];
		
		GLView *v = [[GLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
		self.view = v;
		[v release];
		
		v.context = context;
		[v initViewport];
		
		animating = NO;
		frameInterval = 2;
	}
	return self;
}

-(void)startAnimation {
	if (!animating) {
		CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
		self.dispLink = displayLink;
		[displayLink setFrameInterval:frameInterval];
		[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
		animating = YES;
	}
}

-(void)stopAnimation {
	if (animating) {
		[dispLink invalidate];
		self.dispLink = nil;
		animating = NO;
	}
}

-(void)drawFrame {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
#ifdef PROJECTION_PERSPECTIVE
	gluLookAtf(0.0f, 0.0f, 415.6921938165306f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
	
	// 使用 glDrawArrays 绘图
	glPushMatrix();
	glTranslatef(-80.0f, 120.0f, 0.0f);
	
	static GLfloat rot = -6.6f;
	rot += 6.6f;
	glRotatef(rot, -1.0f, 0.0f, 0.0f);
	
	static const GLfloat vertexs[] = {
		-50.0f, -50.0f, 0.0f,	1.0f, 0.0f, 0.0f, 1.0f,
		50.0f, -50.0f, 0.0f,	0.0f, 1.0f, 0.0f, 1.0f,
		-50.0f, 50.0f, 0.0f,	0.0f, 0.0f, 1.0f, 1.0f,
		50.0f, 50.0f, 0.0f,	1.0f, 1.0f, 1.0f, 1.0f,
	};
	
	glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), vertexs);
	glEnableClientState(GL_VERTEX_ARRAY);
	
	glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), vertexs + 3);
	glEnableClientState(GL_COLOR_ARRAY);
	
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	glPopMatrix();
	
	// 使用 glDrawElements
	glPushMatrix();
	glTranslatef(80.0f, 120.0f, 0.0f);
	
	glRotatef(rot, 0.0f, 1.0f, 0.0f);
	static const GLbyte vertexIdx[] = {
		0, 1, 2, 3,
	};
	
	glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, vertexIdx);
	glPopMatrix();
	
#else
	static GLfloat rot = 0.0f;
	glRotatef(rot, -1.0f, 0.0f, 0.0f);
	rot += 6.6f;
	
	static const GLfloat vertexs[] = {
		-50.0f, -50.0f, 0.0f,	1.0f, 0.0f, 0.0f, 1.0f,
		50.0f, -50.0f, 0.0f,	0.0f, 1.0f, 0.0f, 1.0f,
		-50.0f, 50.0f, 0.0f,	0.0f, 0.0f, 1.0f, 1.0f,
		50.0f, 50.0f, 0.0f,	1.0f, 1.0f, 1.0f, 1.0f,
	};
	
	glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), vertexs);
	glEnableClientState(GL_VERTEX_ARRAY);
	
	glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), vertexs + 3);
	glEnableClientState(GL_COLOR_ARRAY);
	
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
#endif
	
	[(GLView *)self.view presentRenderbuffer];
}

@end
